<template>
  <div>
    <page-list :x-uri="uri" :doactions="doactions" :checkbox-item="false" :custom-components="custom">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'market.coupon.offline.coursecard.export'" size="mini" type="primary" @click="exportCourseCard">导出可用课程卡</el-button>
        <el-button v-auth="'market.coupon.offline.coursecard.create'" size="mini" type="primary" @click="create">新建课程卡</el-button>
      </template>
    </page-list>
  </div>
</template>

<script>
import PageList from '@public/template/page_list.vue';
import OfflineCount from './components/offline_count.vue';
import UseTime from './components/use_time.vue';
import DeriveTime from './components/derive_time.vue';
import CourseInfo from './components/course_info.vue';
import Tabs from '../tabs';
import { Loading } from 'element-ui';
export default {
  components: {
    PageList,
    Tabs
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/course/admin/coupon/offline/courseCard/list',
      doactions: [
        {
          title: '领取详情',
          meta: { permission: 'market.coupon.offline.coursecard.details' },
          callback: ({ row }) => {
            this.$router.push({ path: '/market/coupon/offline/courseCard/' + row.id + '/details' });
          }
        },
        {
          title: '编辑',
          meta: { permission: 'market.coupon.offline.coursecard.edit' },
          callback: ({ row }) => {
            this.$router.push({ path: '/market/coupon/offline/courseCard/' + row.id + '/edit' });
          }
        },
        { title: '删除', meta: { permission: 'market.coupon.offline.coursecard.delete' }, callback: this.deleteUser }
      ],
      custom: {
        offline_count: OfflineCount,
        use_time: UseTime,
        derive_time: DeriveTime,
        course_id: CourseInfo
      }
    };
  },
  methods: {
    create: function() {
      this.$router.push({ path: '/market/coupon/offline/courseCard/create' });
    },
    deleteUser: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/coupon/offline/courseCard/' + row.id + '/delete',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    /**
     * 导出课程卡数据
     */
    exportCourseCard: function() {
      const search = this.$store.getters['pageList/search/data'];
      const loadingInstance = Loading.service({
        text: '正在导出文件，请稍后...'
      });

      this.$http
        .downloadExcel('/course/admin/coupon/offline/courseCard/export', search)
        .then(() => {
          loadingInstance.close();
          this.$message.success('下载成功');
        })
        .catch(msg => {
          loadingInstance.close();
          this.$message.error(msg);
        });
    }
  }
};
</script>
